package com.qf.shiro20230208.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class LoginController {
    @PostMapping("/login")
    public String login(String username, String password,boolean rememberMe){
        //基于shiro的登陆验证
        //subject 代表当前用户信息
        final Subject subject = SecurityUtils.getSubject();

        //使用shiro处理身份验证

        final UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        subject.login(token);

        final HashMap<String, Object> map = new HashMap<>();
        map.put("code","1");
        map.put("msg","ok");
        return "redirect:/home.html";


    }
    @GetMapping("/logout")
    public String logout(){

        final Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.html";

    }
}
